-----------------------------------
-- Area: Xarcabard
-- NPC:  Biast
-- Custom Biast Fight
-----------------------------------

require("scripts/globals/status");
require("scripts/zones/Xarcabard/TextIDs");

-----------------------------------
-- onMobInitialize Action
-----------------------------------

function onMobInitialize(mob)
	mob:addStatusEffect(EFFECT_REGEN,50,3,0);
	mob:addStatusEffect(EFFECT_REFRESH,100,3,0);
	mob:addStatusEffect(EFFECT_REGAIN,10,3,0);
	printf("Biast Initialized");
end;

-----------------------------------
-- onMobEngaged
-----------------------------------

function onMobEngaged(mob,target)

	SetServerVariable("Biast_Phase",1);
	SetServerVariable("Biast_Engaged",os.time(t));
	SetServerVariable("Biast_Rage",0);
	SetServerVariable("Biast_Mode",0);
	SetServerVariable("Biast_Enrage",0);
	mob:delStatusEffect(EFFECT_MAGIC_SHIELD);
	mob:delStatusEffect(EFFECT_MANAFONT);
	mob:delStatusEffect(EFFECT_PHYSICAL_SHIELD);
	mob:delStatusEffect(EFFECT_MIGHTY_STRIKES);
	mob:delStatusEffect(EFFECT_HUNDRED_FISTS);
	mob:delStatusEffect(EFFECT_CHAINSPELL);
	mob:SetMobAbilityEnabled(true);
	mob:SetAutoAttackEnabled(true);
	if(GetMobAction(17236180) ~= 0) then
		DespawnMob(17236180);
	end
	if(GetMobAction(17236059) ~= 0) then
		DespawnMob(17236059);
	end
	printf("Biast engaged");
	
end;

-----------------------------------
-- onMobDisengage
-----------------------------------

function onMobDisengage(mob,target)

	if(GetMobAction(17236180) ~= 0) then
		DespawnMob(17236180);
	end
	if(GetMobAction(17236059) ~= 0) then
		DespawnMob(17236059);
	end
	printf("Biase disengaged");

end;

-----------------------------------
-- onMobFight
-----------------------------------

function onMobFight(mob,target)

	local RandomCast;
	local RandomSpell;
	local SpellNumber;
	local BiastModeCounter;

	-- Check rage timer
	if(GetServerVariable("Biast_Rage") == 0) then
		if(os.time(t) >= (GetServerVariable("Biast_Engaged") + 4800)) then
			mob:rageMode(); -- Stats = Stats * 10
			SetServerVariable("Biast_Rage",1);
			printf("Biast Rage Mode");
		end
	end
	
	-- Set phase
	if((mob:getHP() <= 25000) and (GetServerVariable("Biast_Phase") <= 3)) then
		SetServerVariable("Biast_Phase",4);
		SetServerVariable("Ereshkigal_Timer",os.time(t));
		SpawnMob(17236059):updateEnmity(target);
		mob:addStatusEffect(EFFECT_MIGHTY_STRIKES, 0, 1, 0, 0);
		mob:addStatusEffect(EFFECT_CHAINSPELL, 0, 1, 0, 0);
		mob:addStatusEffect(EFFECT_MANAFONT, 0, 1, 0, 0);
		printf("Biast Phase 4");
	elseif((mob:getHP() <= 50000) and (GetServerVariable("Biast_Phase") <= 2)) then
		SetServerVariable("Biast_Phase",3);
		SetServerVariable("Shadow_Eye_Phase",0);
		SpawnMob(17236180):updateEnmity(target);
		mob:delStatusEffect(EFFECT_MAGIC_SHIELD);
		mob:delStatusEffect(EFFECT_MANAFONT);
		mob:delStatusEffect(EFFECT_PHYSICAL_SHIELD);
		mob:delStatusEffect(EFFECT_MIGHTY_STRIKES);
		mob:delStatusEffect(EFFECT_HUNDRED_FISTS);
		mob:SetMobAbilityEnabled(true);
		mob:SetAutoAttackEnabled(true);
		printf("Biast Phase 3");
	elseif((mob:getHP() <= 75000) and (GetServerVariable("Biast_Phase") == 1)) then
		SetServerVariable("Biast_Phase",2);
		mob:addStatusEffectEx(EFFECT_MAGIC_SHIELD, 0, 1, 0, 0);
		mob:addStatusEffectEx(EFFECT_MANAFONT, 0, 1, 0, 0);
		mob:SetMobAbilityEnabled(false);
		printf("Biast Phase 2");
	end
	
	-- Select phase
	if(GetServerVariable("Biast_Phase") == 1) then
		-- Phase 1
		RandomCast = math.random();
		if(RandomCast >= 0.8) then --Cast every 15 seconds on average
			RandomSpell = math.random();
			if(RandomSpell <= 0.06) then
				SpellNumber = 176; -- Firaga III
			elseif(RandomSpell <= 0.16) then
				SpellNumber = 181; -- Blizzaga III
			elseif(RandomSpell <= 0.3) then
				SpellNumber = 196; -- Thundaga III
			elseif(RandomSpell <= 0.33) then
				SpellNumber = 148; -- Fire V
			elseif(RandomSpell <= 0.38) then
				SpellNumber = 153; -- Blizzard V
			elseif(RandomSpell <= 0.45) then
				SpellNumber = 168; -- Thunder V
			elseif(RandomSpell <= 0.55) then
				SpellNumber = 274; -- Sleepga II
			elseif(RandomSpell <= 0.65) then
				SpellNumber = 227; -- Poisonga III
			elseif(RandomSpell <= 0.75) then
				SpellNumber = 360; -- Dispelga
			elseif(RandomSpell <= 0.8) then
				SpellNumber = 232; -- Bio III
			elseif(RandomSpell <= 0.85) then
				SpellNumber = 246; -- Drain II
			elseif(RandomSpell <= 0.9) then
				SpellNumber = 266; -- Absorb-STR
			elseif(RandomSpell <= 0.95) then
				SpellNumber = 268; -- Absorb-VIT
			else
				SpellNumber = 252; -- Stun
			end
			mob:castSpell(SpellNumber);
			printf("Biast cast spell %u RC: %1.2f, RS: %1.2f",SpellNumber,RandomCast,RandomSpell);
		end
	elseif(GetServerVariable("Biast_Phase") == 2) then
		-- Phase 2
		BiastModeCounter = (GetServerVariable("Biast_Mode") + 1);
		SetServerVariable("Biast_Mode",BiastModeCounter);
		printf("Biast Mode %u",BiastModeCounter);
		if(GetServerVariable("Biast_Mode") == 1) then
			-- Physical Mode
			mob:delStatusEffect(EFFECT_MAGIC_SHIELD);
			mob:delStatusEffect(EFFECT_MANAFONT);
			mob:addStatusEffect(EFFECT_PHYSICAL_SHIELD, 0, 1, 0, 0);
			mob:addStatusEffect(EFFECT_MIGHTY_STRIKES, 0, 1, 0, 0);
			mob:addStatusEffect(EFFECT_HUNDRED_FISTS, 0, 1, 0, 0);
			mob:SetAutoAttackEnabled(true);
			printf("Biast entering Physical Mode");
		elseif(GetServerVariable("Biast_Mode") == 11) then
			-- Magic Mode
			mob:delStatusEffect(EFFECT_PHYSICAL_SHIELD);
			mob:delStatusEffect(EFFECT_MIGHTY_STRIKES);
			mob:delStatusEffect(EFFECT_HUNDRED_FISTS);
			mob:addStatusEffect(EFFECT_MAGIC_SHIELD, 0, 1, 0, 0);
			mob:addStatusEffect(EFFECT_MANAFONT, 0, 1, 0, 0);
			mob:SetAutoAttackEnabled(false);
			printf("Biast entering Magic Mode");
			RandomSpell = math.random();
			if(RandomSpell <= 0.10) then
				SpellNumber = 219; -- Comet
			elseif(RandomSpell <= 0.30) then
				SpellNumber = 496; -- Firaja
			elseif(RandomSpell <= 0.60) then
				SpellNumber = 497; -- Blizzaja
			else
				SpellNumber = 500; -- Thundaja
			end
			mob:castSpell(SpellNumber);
			printf("Biast cast spell %u RS: %1.2f",SpellNumber,RandomSpell);
		elseif(GetServerVariable("Biast_Mode") >= 12) then
			RandomSpell = math.random();
			if(RandomSpell <= 0.10) then
				SpellNumber = 219; -- Comet
			elseif(RandomSpell <= 0.30) then
				SpellNumber = 496; -- Firaja
			elseif(RandomSpell <= 0.60) then
				SpellNumber = 497; -- Blizzaja
			else
				SpellNumber = 500; -- Thundaja
			end
			mob:castSpell(SpellNumber);
			printf("Biast cast spell %u RS: %1.2f",SpellNumber,RandomSpell);
			if(GetServerVariable("Biast_Mode") >= 15) then
				SetServerVariable("Biast_Mode",0);
			end
		end
	elseif(GetServerVariable("Biast_Phase") == 3) then
		-- Phase 3
		mob:setHP(50000);
		RandomCast = math.random();
		if(RandomCast >= 0.6) then --Cast every 9 seconds on average
			RandomSpell = math.random();
			if(RandomSpell <= 0.06) then
				SpellNumber = 176; -- Firaga III
			elseif(RandomSpell <= 0.16) then
				SpellNumber = 181; -- Blizzaga III
			elseif(RandomSpell <= 0.3) then
				SpellNumber = 196; -- Thundaga III
			elseif(RandomSpell <= 0.33) then
				SpellNumber = 148; -- Fire V
			elseif(RandomSpell <= 0.38) then
				SpellNumber = 153; -- Blizzard V
			elseif(RandomSpell <= 0.45) then
				SpellNumber = 168; -- Thunder V
			elseif(RandomSpell <= 0.55) then
				SpellNumber = 274; -- Sleepga II
			elseif(RandomSpell <= 0.65) then
				SpellNumber = 227; -- Poisonga III
			elseif(RandomSpell <= 0.75) then
				SpellNumber = 360; -- Dispelga
			elseif(RandomSpell <= 0.8) then
				SpellNumber = 232; -- Bio III
			elseif(RandomSpell <= 0.85) then
				SpellNumber = 246; -- Drain II
			elseif(RandomSpell <= 0.9) then
				SpellNumber = 266; -- Absorb-STR
			elseif(RandomSpell <= 0.95) then
				SpellNumber = 268; -- Absorb-VIT
			else
				SpellNumber = 252; -- Stun
			end
			mob:castSpell(SpellNumber);
			printf("Biast cast spell %u RC: %1.2f, RS: %1.2f",SpellNumber,RandomCast,RandomSpell);
		end
		if(GetMobAction(17236180) == 0) then
			mob:setHP(22500);
		end
	elseif(GetServerVariable("Biast_Phase") == 4) then
		-- Phase 4
		-- Ereshkigal Timers
		if(os.time(t) >= (GetServerVariable("Ereshkigal_Timer") + 65)) then
			SpawnMob(17236059):updateEnmity(target);
			SetServerVariable("Ereshkigal_Timer",os.time(t));
			if(GetServerVariable("Biast_Enrage") == 1) then
				mob:delStatusEffect(EFFECT_HUNDRED_FISTS);
				SetServerVariable("Biast_Enrage",0);
			end
			printf("Ereshkigal reset");
		elseif((os.time(t) >= (GetServerVariable("Ereshkigal_Timer") + 35)) and (GetServerVariable("Biast_Enrage") == 0)) then
			mob:addStatusEffect(EFFECT_HUNDRED_FISTS, 0, 1, 0, 0);
			mob:addHP(5000);
			SetServerVariable("Biast_Enrage",1);
			DespawnMob(17236059);
			printf("Biast Enrage");
		end
		-- Biast AI
		if(GetServerVariable("Biast_Enrage") == 0) then
			RandomCast = math.random();
			if(RandomCast >= 0.9) then
				RandomSpell = math.random();
				if(RandomSpell <= 0.25) then
					SpellNumber = 47; -- Protect V
				elseif(RandomSpell <= 0.50) then
					SpellNumber = 52; -- Shell V
				elseif(RandomSpell <= 0.75) then
					SpellNumber = 107; -- Phalanx II
				else
					SpellNumber = 340; -- Utsusemi: San
				end
				mob:castSpell(SpellNumber);
				printf("Biast cast spell %u RC: %1.2f, RS: %1.2f",SpellNumber,RandomCast,RandomSpell);
			elseif(RandomCast >= 0.7) then
				RandomSpell = math.random();
				if(RandomSpell <= 0.10) then
					SpellNumber = 496; -- Firaja
				elseif(RandomSpell <= 0.25) then
					SpellNumber = 497; -- Blizzaja
				elseif(RandomSpell <= 0.45) then
					SpellNumber = 500; -- Thundaja
				elseif(RandomSpell <= 0.55) then
					SpellNumber = 118; -- Nasty Spell (Void Storm)
				elseif(RandomSpell <= 0.60) then
					SpellNumber = 218; -- Meteor
				elseif(RandomSpell <= 0.80) then
					SpellNumber = 274; -- Sleepga II
				elseif(RandomSpell <= 0.90) then
					SpellNumber = 611; -- Disseverment
				else
					SpellNumber = 531; -- Ice Break
				end
				mob:castSpell(SpellNumber);
				printf("Biast cast spell %u RC: %1.2f, RS: %1.2f",SpellNumber,RandomCast,RandomSpell);
				if(SpellNumber == 118) then
					RandomSpell = math.random();
					if(RandomSpell <= 0.2) then
						target:addStatusEffect(EFFECT_WEAKNESS, 0, 1, 30, 0);
					elseif(RandomSpell <= 0.4) then
						target:addStatusEffect(EFFECT_COSTUME, 421, 1, 30, 0);
					elseif(RandomSpell <= 0.6) then
						target:addStatusEffect(EFFECT_SJ_RESTRICTION, 0, 1, 30, 0);
					elseif(RandomSpell <= 0.8) then
						target:addStatusEffect(EFFECT_STUN, 0, 1, 30, 0);
					else
						target:addStatusEffect(EFFECT_LEVEL_RESTRICTION, 70, 1, 30, 0);
					end
					printf("Biast inflicts status, RS: %1.2f",RandomSpell);
				end
			end
		else
			target:addStatusEffect(EFFECT_WEIGHT, 50, 1, 30, 0);
			RandomCast = math.random();
			if(RandomCast >= 0.8) then
				RandomSpell = math.random();
				if(RandomSpell <= 0.5) then
					SpellNumber = 274; -- Sleepga II
				else
					SpellNumber = 218; -- Meteor
				end
				mob:castSpell(SpellNumber);
				printf("Biast cast spell %u RC: %1.2f, RS: %1.2f",SpellNumber,RandomCast,RandomSpell);
			end
			printf("Biast inflicts weight");
		end
	end
	printf("Biast HP: %u, MP: %u, TP: %u",mob:getHP(),mob:getMP(),mob:getTP())

end;

-----------------------------------
-- onMobDeath
-----------------------------------

function onMobDeath(mob,killer)

	
	
end;

-----------------------------------
-- onEventUpdate
-----------------------------------

function onEventUpdate(player,csid,option)
--printf("updateCSID: %u",csid);
--printf("RESULT: %u",option);
end;

-----------------------------------
-- onEventFinish
-----------------------------------

function onEventFinish(player,csid,option)
--printf("finishCSID: %u",csid);
--printf("RESULT: %u",option);
end;